package com.fs.controller.v1;

import com.fs.controller.PicFitUtil;
import com.fs.controller.vo.shop.RespGetPics;
import com.fs.global.config.CustomConfig;
import com.fs.controller.vo.shop.ReqCreateShop;
import com.fs.controller.vo.shop.RespShop;
import com.fs.global.init.RoleName;
import com.fs.service.ShopService;
import com.fs.service.bo.shop.BoCreateShopParam;
import com.fs.service.bo.shop.BoShop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Api(tags = "门店API", description = "管理员账户使用")
@RestController("ShopControllerV1")
@RequestMapping("/v1/shop")
@RequiresRoles(RoleName.ADMIN)
@Validated
public class ShopController {

    private final ShopService shopService;
    private final PicFitUtil picFitUtil;
    private final CustomConfig customConfig;

    public ShopController(ShopService shopService, CustomConfig customConfig, PicFitUtil picFitUtil) {
        this.shopService = shopService;
        this.picFitUtil = picFitUtil;
        this.customConfig = customConfig;
    }

    @ApiOperation("创建门店")
    @PostMapping
    public RespShop createShop(@RequestBody @Valid ReqCreateShop req) {
        BoCreateShopParam param = new BoCreateShopParam().setName(req.getName()).setLatitude(req.getLatitude()).setLongitude(req.getLongitude());
        BoShop shop = shopService.createShop(param);
        return new RespShop(shop);
    }

    @ApiOperation("上传门店图片")
    @PostMapping("/upload")
    public String uploadPic(@RequestParam(value = "file") MultipartFile[] files, Integer shopId)
            throws IOException {

        shopService.getShopByID(shopId);
        String fileSaveDir = picFitUtil.getAbsImageDirOfShop(shopId);

        for (MultipartFile file : files) {
            InputStream inputStream = file.getInputStream();
            OutputStream outputStream = new FileOutputStream(fileSaveDir + File.separator + file.getOriginalFilename());
            byte[] bytes = new byte[1024];
            while (inputStream.read(bytes) != -1) {
                outputStream.write(bytes);
            }
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        }
        return "上传成功";
    }

    @ApiOperation("获取门店照片")
    @GetMapping("/pics")
    public RespGetPics getShopPics(@RequestParam Integer shopId) {
        String fileSaveDir = picFitUtil.getAbsImageDirOfShop(shopId);

        File dir = new File(fileSaveDir);
        String[] files = dir.list();
        if (files == null) {
            return new RespGetPics().setBaseUrl(customConfig.getPicfitAddress());
        }

        List<String> pics = Stream.of(files).map(
                picName -> String.join(File.separator, Arrays.asList(picFitUtil.getShopSavePicDir(), shopId.toString(), picName))
        ).collect(Collectors.toList());
        return new RespGetPics().setBaseUrl(customConfig.getPicfitAddress()).setPics(pics);
    }
}
